/**
 * Created by IBM on 2015/5/31.
 */
var fs = require('fs');
var http = require("http");
module.exports=Controller("Admin/BaseController", function(){
    return {
        indexAction: function () {
            this.assign({
                active: "ufinder"
            });
            this.display();
        },
        ufinderAction: function () {
            var conf = this.conf=F("ufinderconfig");
            var cmd = this.get("cmd");
            var target = this.get("target");
            var result;
            switch (cmd) {
                case 'init':
                    //console.log(conf["ROOT"]);
                    //console.log(isDir("static"));
                    var data = {
                        "root":this.getFileInfo(conf["ROOT"]),
                        "config":[]
                    };
                    result = this.getJson("0", "success", data)
                    //result = {"state": state, "message": message, "data": data};
                    this.json(result);
                    break;
                case 'ls':
                    console.log(target);
                    var list = this.listFile(conf["ROOT"]);
                    console.log(list);
                    var data = {
                        "files":list
                    };
                    result = this.getJson("0", "success", data);
                    this.json(result);
                    break;
                //case 'rename':
                //    $name = $_GET['name'];
                //    if( file_exists($ROOT.$name) ) {
                //        $res = false;
                //        $msg = 'file exist';
                //    } else {
                //        $res = rename($ROOT.$target, $ROOT.$name);
                //    }
                //    if($res) {
                //        echo getJson('0', 'success', array('file' => getFileInfo($name, $ROOT)));
                //    } else {
                //        echo getJson('1', $msg ? $msg:'rename error');
                //    }
                //    break;
                //case 'rm':
                //    foreach($target as $key => $path) {
                //    if(is_dir($ROOT.$path)) {
                //        $res = removeDir($ROOT.$path);
                //    } else {
                //        $res = unlink($ROOT.$path);
                //    }
                //    if(!$res) break;
                //}
                //
                //    if($res) {
                //        echo getJson('0', 'success');
                //    } else {
                //        echo getJson('1', 'romove error');
                //    }
                //    break;
                //case 'touch':
                //    if(!file_exists($ROOT.$target)) {
                //        file_put_contents($ROOT.$target, '');
                //        $res = file_exists($ROOT.$target);
                //    } else {
                //        $res = false;
                //        $msg = 'file exist';
                //    }
                //    if($res) {
                //        echo getJson('0', 'success', array('file' => getFileInfo($target, $ROOT)));
                //    } else {
                //        echo getJson('1', $msg ? $msg:'touch error');
                //    }
                //    break;
                //case 'mkdir':
                //    if(!file_exists($ROOT.$target)) {
                //        $res = mkdir($ROOT.$target);
                //    } else {
                //        $res = false;
                //        $msg = 'file exist';
                //    }
                //    if($res) {
                //        echo getJson('0', 'success', array('file' => getFileInfo($target, $ROOT)));
                //    } else {
                //        echo getJson('1', $msg ? $msg:'mkdir error', array('file' => getFileInfo($target, $ROOT)));
                //    }
                //    break;
                //case 'upload':
                //    include "Uploader.class.php";
                //    $uploadConfig = array(
                //            "savePath" => $ROOT.$target,          //存储文件夹
                //        "maxSize" => 200000,                   //允许的文件最大尺寸，单位KB
                //    "allowFiles" => array(".rar", ".zip", ".7z", "tar", "gz",
                //    ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx",
                //    "", ".txt", ".pdf",
                //    ".bmp", ".gif", ".jpg", ".jpeg", ".png", ".psd",
                //    ".swf", ".mkv", ".avi", ".rm", ".rmvb",
                //    ".mpeg", ".mpg", ".ogg", ".mov", ".wmv", ".mp4", ".webm")  //允许的文件格式
                //);
                //
                //    $up = new Uploader( "file", $uploadConfig );
                //    $info = $up->getFileInfo();
                //    if($info["state"] == 'SUCCESS') {
                //        echo getJson('0', 'success', array('file' => getFileInfo($target.$info["name"], $ROOT)));
                //    } else {
                //        echo getJson('1', $info["state"], array('file' => getFileInfo($target.$info["name"], $ROOT)));
                //    }
                //    break;
                //case 'download':
                //    $path = $ROOT.$target;
                //    $info = getFileInfo($target, $ROOT);
                //    downloadFile($path, $info['name']);
                //    break;
                //case 'info':
                //    var data = {
                //        "file":getFileInfo()
                //    };
                //    result = this.getJson("0", "success", data)
                //    echo getJson('0', 'success', array('file' => getFileInfo($target, $ROOT)));
                //    break;
                default:
                    result = {
                        state: '请求地址出错'
                    };
                    break;
            }
            //返回结果
            //this.json(result);
        },
        getJson: function (state, message, data) {
            var json = {"state": state, "message": message, "data": data};
            return json;
        },
        getFileInfo: function (path) {
            var self = this;
            //var stat = fs.statSync(path);
            ////transdate(stat.ctime);
            //console.log();
            //var info = {
            //    "path": path,
            //    "name": false,
            //    "isdir": isDir(path),
            //    "type": "dir",
            //    "read": self.conf["read"],
            //    "write": self.conf["write"],
            //    "time": stat["ctime"].valueOf(),
            //    "mode": stat["mode"],
            //    "size": stat["size"]
            //}
            return info={"path":"\/","name":false,"isdir":true,"type":"dir","read":true,"write":true,"time":1433578256,"mode":"40777","size":4096};
        },
        listFile:function(path){
           // var fileList = []
           //var files =  fs.open(path,"r+");
           // files = fs.readdirSync(RESOURCE_PATH+"/"+path);
                //files=scanFolder(path);
            //console.log(files);
            //files.forEach(function (item) {
            //    var tmpPath = path + '/' + item,
            //        stats = fs.statSync(RESOURCE_PATH+"/"+tmpPath);
            //        fileList.push(tmpPath);
            //});
            //console.log(files);
           // var files = [{"path":"\/a.apk","name":"a.apk","isdir":false,"type":"file","read":true,"write":true,"time":1433172886,"mode":"100666","size":0},{"path":"\/a.doc","name":"a.doc","isdir":false,"type":"file","read":true,"write":true,"time":1433172886,"mode":"100666","size":0}];
            return files=[{"path":"\/a.apk","name":"a.apk","isdir":false,"type"
                :"file","read":true,"write":true,"time":1433172886,"mode":"100666","size":0},{"path":"\/a.doc","name"
                :"a.doc","isdir":false,"type":"file","read":true,"write":true,"time":1433172886,"mode":"100666","size"
                :0},{"path":"\/a.exe","name":"a.exe","isdir":false,"type":"file","read":true,"write":true,"time":1433172886
                ,"mode":"100777","size":0},{"path":"\/pers\/","name"
                :"pers","isdir":true,"type":"dir","read":true,"write":true,"time":1433578256,"mode":"40777","size":0
            }];
        }
    //    ,
    //    function listFile($dir, $ROOT){
    //    if ($handle = opendir($ROOT.$dir)){
    //        $output = array();
    //        $dir = $dir[strlen($dir) - 1] == '/' ? $dir:$dir.'/';
    //        while (false !== ($item = readdir($handle))){
    //            if ($item != "." && $item != ".."){
    //                $output[] = getFileInfo($dir.$item, $ROOT);
    //            }
    //        }
    //        closedir($handle);
    //        return $output;
    //    }else{
    //        return false;
    //    }
    //}
    }
});
